<?php require('../data/dbcon.php'); ?>
<?php require('../data/functions.php'); ?>
<?php
session_start();
$UserChk = checkUser($_COOKIE['winnguestbook_u'], $_COOKIE['winnguestbook_auth']);
if( $UserChk == FALSE ) {
	header("location: index.php");
}
?>
<?php

//run delete for posts that are 5 days old...
// you may edit this area ONLY IF YOU KNOW WHAT YOUR DOING.

//set todays date
$fivedays = date("mdY");
//run the function
deletePoststhatareold($fivedays);

// END EDIT AREA

if( isset($_GET['approve']) ) {
	approvePost($_GET['approve']);
	header("location: archive.php");
}

if( !empty($_GET['delete']) ) {
	$deletepost = deletePost($_GET['delete']);
		header("location: archive.php?undodel={$_GET['delete']}");
}

if( isset($_GET['undod']) ) {
	undodeleteARCH($_GET['undod']);
	header("location: archive.php");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">@import url('css/admin.css'); </style>

<script type="text/javascript" src="js/prototypejs.js"></script>
<script type="text/javascript" src="js/scriptaculous.js"></script>
<script language="javascript" type="text/javascript">
<!--
Event.observe(window, 'load', init, false);
function init(){
	<?php echo makeEditableARCH(); ?>
}
//-->
</script>
<title>Winn Guestbook <?=$version; ?></title>
</head>

<body>
<div id="adminheader">
	<?php include('inc/sitename.php'); ?>
    <ul id="arch">
<?php include('inc/header.php'); ?>

<div class="content">
        <?php if( !empty($_GET['undodel']) ) { ?>
        	<p id="undo">Did you make a mistake? You can <a href="archive.php?undod=<?=$_GET['undodel']; ?>">undo</a> this.</p>
        <?php } ?>
        <h3>Archives</h3>
        <p class="highlight">You may edit the posts on this page by clicking the text. <span class="red">The posts marked for deletion will be deleted when they are five days old.</span></p>
        <?php echo getALLARChPosts(); ?>
    
</div>
<script type="text/javascript">
<!--
function makeEditable(id){
	Event.observe(id, 'click', function(){edit($(id))}, false);
	//Event.observe(id, 'mouseover', function(){showAsEditable($(id))}, false);
//	Event.observe(id, 'mouseout', function(){showAsEditable($(id), true)}, false);
}

function edit(obj){
	Element.hide(obj);
	
	var textarea = '<div id="'+obj.id+'_editor" class="editclass"><textarea class="edittext" id="'+obj.id+'_edit" name="'+obj.id+'" rows="4" cols="100%">'+obj.innerHTML+'</textarea>';
	var button	 = '<div><input id="'+obj.id+'_save" type="button" value="Save &raquo;" /> OR <a id="'+obj.id+'_cancel" href="javascript://">Close</a></div></div>';
	
	new Insertion.After(obj, textarea+button);	
		
	Event.observe(obj.id+'_save', 'click', function(){saveChanges(obj)}, false);
	Event.observe(obj.id+'_cancel', 'click', function(){cleanUp(obj)}, false);
	
}

function showAsEditable(obj, clear){
	if (!clear){
		Element.addClassName(obj, 'editable');
	}else{
		Element.removeClassName(obj, 'editable');
	}
}

function saveChanges(obj){

	var new_content	=  escape($F(obj.id+'_edit'));

	obj.innerHTML	= "Saving...";
	cleanUp(obj, true);

	var success	= function(t){editComplete(t, obj);}
	var failure	= function(t){editFailed(t, obj);}

  	var url = 'editpost.php';
	var pars = 'postid='+obj.id+'&content='+new_content;
	var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});

}

function cleanUp(obj, keepEditable){
	Element.remove(obj.id+'_editor');
	Element.show(obj);
	if (!keepEditable) showAsEditable(obj, true);
}

function editComplete(t, obj){
	obj.innerHTML	= t.responseText;
	showAsEditable(obj, true);
}

function editFailed(t, obj){
	obj.innerHTML	= 'Sorry, the update failed.';
	cleanUp(obj);
}


//-->
</script>
<?php include('inc/footer.php');?>
</body>
</html>
